//
//  YBClassBasicInfoViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 2019/4/26.
//  Copyright © 2019 apple. All rights reserved.
//

#import "YBClassBasicInfoViewController.h"
#import "YBClassBasicInfoTableViewCell.h"
#import "YBClassBasicInfoFooterView.h"


#define CELLID @"YBClassBasicInfoTableViewCell"
#define FOOTERID @"YBClassBasicInfoFooterView"

@interface YBClassBasicInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UIImageView * headImageView;

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , assign) CGFloat itemHeight;

@property (nonatomic , strong) NSArray * dataArray;

@property (nonatomic , strong) NSArray * titleArray;

@property (nonatomic , strong) YBClassBasicInfoModel * model;

@end

@implementation YBClassBasicInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ybTitleString = @"基本信息";
    self.itemHeight = [@"尚锟" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(16)} context:nil].size.height + 1;
    self.titleArray = @[@"班级名称：" , @"班级编号：" , @"班级时间：" , @"主办单位：" , @"承办单位：" , @"报名人数："];
    self.dataArray = @[@"暂无" , @"暂无" , @"暂无" , @"暂无" , @"暂无" , @"暂无"];
    [self initUserInterface];
    [self initDataSource];
}

-(void)initUserInterface
{
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .topSpaceToView(self.navigView, 0)
    .bottomSpaceToView(self.view, 0);
}

-(void)initDataSource{
    [YJProgressHUD showMessage:@"加载中..." duration:10.f view:self.view];
    [YBRequestManager request_classBasicInfoWithIdString:self.idString successBlock:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"code"] integerValue] == 1) {
            if ([object[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray * tempArray = object[@"data"];
                if (tempArray.count > 0) {
                    NSDictionary * tempDic = tempArray[0];
                    self.model = [YBClassBasicInfoModel yy_modelWithJSON:tempDic];
                    [self.headImageView sd_setImageWithURL:CREATEURL(self.model.image_servername) placeholderImage:PlaceholderBIGImage];
                    NSString * timeString = @"暂无";
                    if ((self.model.starttime.length > 0 || ![self.model.starttime isEqualToString:@"<null>"]) && (self.model.endtime.length > 0 || ![self.model.endtime isEqualToString:@"<null>"])) {
                        timeString = [NSString stringWithFormat:@"%@-%@" , self.model.starttime , self.model.endtime];
                    }
                    if ((self.model.starttime.length > 0 || ![self.model.starttime isEqualToString:@"<null>"]) && (self.model.endtime.length == 0 || [self.model.endtime isEqualToString:@"<null>"])) {
                        timeString = [NSString stringWithFormat:@"%@" , self.model.starttime];
                    }
                    if ((self.model.starttime.length == 0 || [self.model.starttime isEqualToString:@"<null>"]) && (self.model.endtime.length > 0 || ![self.model.endtime isEqualToString:@"<null>"])) {
                        timeString = [NSString stringWithFormat:@"%@" ,  self.model.endtime];
                    }
                    
                    self.dataArray = @[
                                       [NSString stringWithFormat:@"%@" , self.model.name] ,
                                       [NSString stringWithFormat:@"【%@】" , self.model.code] ,
                                       timeString ,
                                       [NSString stringWithFormat:@"%@" , self.model.organizer] ,
                                       [NSString stringWithFormat:@"%@" , self.model.undertaker] ,
                                       [NSString stringWithFormat:@"%@名" , self.model.signnumber]];
                    [self.tableView reloadData];
                }
            }
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * infoString = self.dataArray[indexPath.row];
    if (infoString.length > 0) {
        CGFloat cellHeight = [infoString boundingRectWithSize:CGSizeMake([UIScreen zj_screenWith] - ItemHeight * 280 - 1, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(16)} context:nil].size.height + 1;
        return ItemHeight * 30 + cellHeight;
    }
    else
    {
        return ItemHeight * 30 + self.itemHeight;
    }
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YBClassBasicInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    [cell setCellInfoWithTitle:self.titleArray[indexPath.row] infoString:self.dataArray[indexPath.row]];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    YBClassBasicInfoFooterView * footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:FOOTERID];
    [footer setInfoWithMustNumber:self.model.requirecount mustTime:self.model.requiredstudytime chooseNumber:self.model.electivecount chooseTime:self.model.electivestudytime];
    return footer;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return ItemHeight * 160;
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableHeaderView = self.headImageView;
        [_tableView registerClass:[YBClassBasicInfoTableViewCell class] forCellReuseIdentifier:CELLID];
        [_tableView registerClass:[YBClassBasicInfoFooterView class] forHeaderFooterViewReuseIdentifier:FOOTERID];
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}

- (UIImageView *)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenWith] / 2)];
        _headImageView.image = PlaceholderBIGImage;
        _headImageView.contentMode = UIViewContentModeScaleToFill;
    }
    return _headImageView;
}

-(YBClassBasicInfoModel *)model
{
    if (!_model) {
        _model = [[YBClassBasicInfoModel alloc]init];
    }
    return _model;
}

@end
